package com.power.using.sharlockchat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.http.Request;
import com.power.using.sharlockchat.interfaces.Constant;
import com.power.using.sharlockchat.utils.RegexUtils;
import com.power.using.sharlockchat.utils.SPUtil;
import com.power.using.sharlockchat.utils.ToastUtils;
import com.power.using.sharlockchat.utils.loading.LoadingDialog;

/**
 * Created by 聪明一只哈 on 2018/5/30.
 * Time: 2018/5/30  11:08
 */

public class LoginActivity extends AppCompatActivity {
    EditText tvLoginName;
    EditText tvLoginPass;
    Button btLogin;
    private LoginRequest loginRequest;
    private String login_name;
    private String login_pass;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tvLoginName=(EditText)findViewById(R.id.tv_login_name);
        tvLoginPass=(EditText)findViewById(R.id.tv_login_pass);
        btLogin=(Button)findViewById(R.id.bt_login);


    }

    /**
     * 点击登录
     */

    public void onClicked(View view) {


        login_name = tvLoginName.getText().toString().trim();
        login_pass = tvLoginPass.getText().toString().trim();

        if (TextUtils.isEmpty(login_name)) {
            ToastUtils.showLongToast(this,"手机号有误，请重新输入");
            return;
        }
        if (TextUtils.isEmpty(login_pass)) {
            Toast.makeText(LoginActivity.this, "密码有误，请重新输入", Toast.LENGTH_SHORT).show();
            return;
        }


        //手机号正则校验
        if (!RegexUtils.isMobileExact(login_name)) {

            Toast.makeText(LoginActivity.this, "手机号格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }

        LoadingDialog.showLoadingDialog(this,"正在登录...");

        SPUtil.putString(Constant.KEY_USERNAME, login_name);
        SPUtil.putString(Constant.KEY_USERPWD, login_pass);
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
        LoadingDialog.cancelLoadingDialog();


//        String mBuyUrl = HttpApis.login;
//
//        HashMap<String ,String> map=new HashMap<>();
//        map.put("name", login_name);
//        map.put("password", login_pass);
//        if (loginRequest==null){
//            loginRequest = new LoginRequest();
//        }
//        HttpUtils.requestPost(this, mBuyUrl, map, loginRequest);

    }






    /**
     * 登录请求数据
     */
    private class LoginRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);
            Log.i("测试数据", "success: " + jsonStr);

//            UserEntity loginEntity = GsonTools.changeGsonToBean(jsonStr, UserEntity.class);
//            //数据解析
//            processData(loginEntity);
//            cancelLoadingDialog();
        }

        @Override
        public void error(String error) {
            super.error(error);

            Log.i("测试数据", "error: " + error);
            //showShortToast("用户名或密码有误，请重新输入");
//            showShortToast("网络繁忙,请稍后再试");
//
//            cancelLoadingDialog();

        }
    }

    /**
     * 数据解析
     * @param userEntity
     */
//    private void processData(UserEntity userEntity) {
//        if (userEntity != null) {
//            saveInfor(userEntity);
//
//            UserEntity userEntity1 = MyApplication.getUserEntity();
//            if (userEntity1!=null){
//                //没有状态码,先用假条件登录
//                if (!"".equals(userEntity.getOpt_id())){
//                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
//                    finish();
//                }else {
//                    showShortToast("用户名或密码有误，请重新输入");
//                }
//            }else {
//                showShortToast("用户名或密码有误，请重新输入");
//            }
//
//
//        }
//    }

    /**
     * 保存个人信息 存到application中,方便获取
     * @param userEntity
     */
//    private void saveInfor(UserEntity userEntity) {
//        //姓名
//        SPUtil.putString(Constants.KEY_USERNAME, login_name);
//        //不保存登录密码
//        SPUtil.putString(Constant.KEY_USERPWD, login_pass);
//        MyApplication.setUserEntity(userEntity);
//        SPUtil.putBeanValue(Constants.KEY_USER, userEntity);
//
//    }


}
